package me.cxlr.shaopinghelper.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.cardview.widget.CardView;
import androidx.core.content.res.ResourcesCompat;

import me.cxlr.shaopinghelper.R;

/**
 * 控件--卡片式显示组件
 *
 * @author ShanLunZhi
 */
public class CardViewShowMsgWidget extends LinearLayoutCompat {
    private Context context;
    private View view;
    private CardView cardView;
    private LinearLayoutCompat llcRoot;
    private AppCompatImageView imageView;
    private AppCompatTextView textViewTitle;
    private AppCompatTextView textViewMessage;

    public CardViewShowMsgWidget(@NonNull Context context) {
        super(context);
        this.context = context;
        initWidget();
    }

    public CardViewShowMsgWidget(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.context = context;
        initWidget();
    }

    private void initWidget() {
        view = LayoutInflater.from(context).inflate(R.layout.widget_cardview_show_msg, this);
        cardView = view.findViewById(R.id.wcsm_cv);
        llcRoot = view.findViewById(R.id.wcsm_ll_root);
        imageView = view.findViewById(R.id.wcsm_img);
        textViewTitle = view.findViewById(R.id.wcsm_tv_title);
        textViewMessage = view.findViewById(R.id.wcsm_tv_message);

        cardView.setRadius(35F);
    }

    public void setImage(int resourceId) {
        Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), resourceId, null);
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
    }

    public void setImage(Drawable drawable) {
        if (drawable != null) {
            imageView.setImageDrawable(drawable);
        }
    }

    public void setTitle(int resourceId) {
        textViewTitle.setText(resourceId);
    }

    public void setTitle(CharSequence title) {
        textViewTitle.setText(title);
    }

    public void setMessage(int resourceId) {
        textViewMessage.setText(resourceId);
    }

    public void setMessage(CharSequence title) {
        textViewMessage.setText(title);
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        llcRoot.setOnClickListener(onClickListener);
    }

    @Override
    public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
        llcRoot.setOnLongClickListener(onLongClickListener);
    }
}
